package flst.ipiweb.struts.action.livraison;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import flst.ipiweb.service.ServiceLivraison;
import flst.ipiweb.transverse.IPIException;
import flst.ipiweb.transverse.Relai;

public class DeleteRelaiAction extends Action{

	public ActionForward execute(
			ActionMapping mapping, 
			ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response)
			throws Exception {
		
			//On récupère la session
			HttpSession session = request.getSession();
			//Si l'administrateur n'est pas logué 
			//on le redirige sur le formulaire de login
			if(session.getAttribute("login") == null)
			{
				return mapping.findForward("login");
			}
			
			Relai relai = new Relai();
			int idRelai = Integer.parseInt(request.getParameter("id"));
			relai.setIdRelai(idRelai);
			ServiceLivraison sLivraison = new ServiceLivraison();
			try
			{
				sLivraison.deleteRelai(relai);
				//Message de succès
				session.setAttribute("success","RelaiSuccess.delete");
				session.setAttribute("error", null);
			}
			catch(IPIException e)
			{
				session.setAttribute("success",null);
				session.setAttribute("error", e.getMessage());
			}
	
			return mapping.findForward("list_relai");
		}
}
